package com.lovo.studentManagement;

import java.util.Scanner;

/**
 * 学生管理主界面
 */
public class StudentUI {
    Scanner scanner = new Scanner(System.in);
    SuperArray superArray = new SuperArray();
    /**
     * 菜单
     */
    public void menu(){
        int index;
        while (true){
            System.out.println("\n1-查找学生 2-添加学生 3-删除学生 4-修改学生 5-退出");
            index = scanner.nextInt();
            switch (index){
                case 1:
                    lookup();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    del();
                    break;
                case 4:
                    change();
                    break;
                case 5:
                    System.exit(1);
                default:
                    System.out.println("输入错误");
                    continue;
            }
        }
    }


    /**
     * 添加学生
     */
    public void add(){
        System.out.println("请输入学生姓名：");
        String name = scanner.next();
        System.out.println("请输入学生年龄：");
        int age = scanner.nextInt();
        System.out.println("请输入学生性别：");
        String gender = scanner.next();
        Student student = new Student(name, age, gender);

        superArray.add(student);
    }

    /**
     * 删除学生
     */
    public void del(){
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        for (int i = 0; i < superArray.size(); i++){
            Student student =  (Student) superArray.get(i);
            if (student.getName().equals(name)){
                superArray.remove(i);
                System.out.println("删除成功");
                return;
            }

        }
        System.out.println("未查找到相关学生");


    }

    /**
     * 修改学生
     */
    public void change(){
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        for (int i = 0; i < superArray.size(); i++){
            Student student =  (Student) superArray.get(i);
            if (student.getName().equals(name)){
                superArray.remove(i);
                add();
                System.out.println("修改成功");
                return;
            }

        }
        System.out.println("未查找到相关学生");
    }

    /**
     * 查找学生
     */
    public void lookup(){
        System.out.println("请输入学生姓名（如要查询所有学生请输入1）");
        String name = scanner.next();
        if (name.equals("1")){
            for (int i = 0; i < superArray.size(); i++){
                Student student =  (Student) superArray.get(i);
                    System.out.println("姓名：" + student.getName() + "，年龄：" + student.getAge()
                            + "，性别：" + student.getGender());
            }
        }else {
            for (int i = 0; i < superArray.size(); i++){
                Student student =  (Student) superArray.get(i);
                if (student.getName().equals(name)){
                    System.out.println("姓名：" + student.getName() + "，年龄：" + student.getAge()
                            + "，性别：" + student.getGender());
                    return;
                }

            }
            System.out.println("未查找到相关学生");
        }

    }

    public static void main(String[] args) {
        StudentUI studentUI = new StudentUI();
        studentUI.menu();
    }

}

